﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PagePermission : Form
    {
        public PagePermission()
        {
            InitializeComponent();
        }
        
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        bool isfirst = true;
        private char char0 ;
        private char charPassword = '*';
        private void PagePermission_Load(object sender, EventArgs e)
        {
            changeUser.changeUserMain();
          var passwordChar = '*';
           if( MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.superAdmin)
            {
                tbx_password_super.ReadOnly = false;
                tbx_password_super.PasswordChar = char0;
                tbx_password_admin.ReadOnly = false;
                tbx_password_admin.PasswordChar = char0;
                tbx_password_tech.ReadOnly = false;
                tbx_password_tech.PasswordChar = char0;
                tbx_password_operate.ReadOnly = false;
                tbx_password_operate.PasswordChar = char0;
           
                tbx_password_super.Text = GlobalVar.closeSave.password_super;
                tbx_password_admin.Text = GlobalVar.closeSave.password_admin;
                tbx_password_tech.Text = GlobalVar.closeSave.password_tech;
                tbx_password_operate.Text = GlobalVar.closeSave.password_operate;
                
            }
            if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.admin)
            {
                tbx_password_super.ReadOnly = true;
                tbx_password_super.Text = GlobalVar.closeSave.password_super;
                tbx_password_super.PasswordChar = passwordChar;
     


                tbx_password_admin.ReadOnly = false;
                tbx_password_admin.PasswordChar = char0;
                tbx_password_admin.Text = GlobalVar.closeSave.password_admin;

                tbx_password_tech.ReadOnly = false;
                tbx_password_tech.PasswordChar = char0;
                tbx_password_tech.Text = GlobalVar.closeSave.password_tech;


                tbx_password_operate.ReadOnly = false;
                tbx_password_operate.PasswordChar = char0;
                tbx_password_operate.Text = GlobalVar.closeSave.password_operate;

            }
            if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.technician)
            {
                tbx_password_super.ReadOnly = true;
                tbx_password_super.Text = GlobalVar.closeSave.password_super;
                tbx_password_super.PasswordChar = passwordChar;
               

                tbx_password_admin.ReadOnly = true;
                tbx_password_admin.Text = GlobalVar.closeSave.password_admin;
                tbx_password_admin.PasswordChar = passwordChar;
              

                tbx_password_tech.ReadOnly = false;
                tbx_password_tech.PasswordChar = char0;
                tbx_password_tech.Text = GlobalVar.closeSave.password_tech;


                tbx_password_operate.ReadOnly = false;
                tbx_password_operate.PasswordChar = char0;
                tbx_password_operate.Text = GlobalVar.closeSave.password_operate;
            }
            if (MotionControl.GlobalVariable.OperLevel == (int)MotionControl.operLevel.operatorStaff)
            {
                tbx_password_super.ReadOnly = true;
                tbx_password_super.Text = GlobalVar.closeSave.password_super;
                tbx_password_super.PasswordChar = passwordChar;


                tbx_password_admin.ReadOnly = true;
                tbx_password_admin.Text = GlobalVar.closeSave.password_admin;
                tbx_password_admin.PasswordChar = passwordChar;


                tbx_password_tech.ReadOnly = true;
                tbx_password_tech.PasswordChar = passwordChar;
                tbx_password_tech.Text = GlobalVar.closeSave.password_tech;


                tbx_password_operate.ReadOnly = false;
                tbx_password_operate.PasswordChar = char0;
                tbx_password_operate.Text = GlobalVar.closeSave.password_operate;
            }
       
            if (isfirst)
            {
                isfirst = false;
                ckb_IsSaftGate.Checked = GlobalVar.closeSave.IsSaftGate;
                ckb_IsSaftGate.CheckedChanged += Ckb_IsSaftGate_CheckedChanged;
                ckb_downPro.Checked = GlobalVar.IsRealsePro;
                ckb_IsManThrough.Checked= GlobalVar.IsManThrought;
                ckb_IsOldTest.Checked = GlobalVar.IsOldTest;
                ckb_isThrough.Checked = GlobalVar.IsThrought;

            }
         
        }

       

        private void Ckb_IsSaftGate_CheckedChanged(object sender, EventArgs e)
        {
            GlobalVar.closeSave.IsSaftGate = ckb_IsSaftGate.Checked;
        }

        private void ckb_IsOldTest_CheckedChanged(object sender, EventArgs e)
        {
            GlobalVar.IsOldTest = ckb_IsOldTest.Checked;
        }

        private void ckb_isThrough_CheckedChanged(object sender, EventArgs e)
        {
            GlobalVar.IsThrought = ckb_isThrough.Checked;
        }

        private void ckb_IsManThrough_CheckedChanged(object sender, EventArgs e)
        {
            GlobalVar.IsManThrought = ckb_IsManThrough.Checked;
        }

        private async void btn_connect_Click(object sender, EventArgs e)
        {
            btn_connect.Enabled = false;
            
            await Task.Run(() =>
            {
                GlobalVar.isExit = true;
                Thread.Sleep(2000);
                var res = GlobalMotion.Card_1.SoftReset((ushort)GlobalMotion.Card_1.m_CardID);
                if (res != -1)
                {
                    MessageBox.Show("已能正常连接运动控制卡,请重启软件生效");
                }
            });
            btn_connect.Enabled = true;
        }

        private void btn_ManSignal_Click(object sender, EventArgs e)
        {
            GlobalVar.IsManFinishSignal = true;
        }

       



        private void ckb_downPro_CheckedChanged(object sender, EventArgs e)
        {
            GlobalVar.IsRealsePro = ckb_downPro.Checked;
        }

        private void btn_SaveClose_Click(object sender, EventArgs e)
        {
            GlobalVar.closeSave.password_admin = tbx_password_admin.Text;
            GlobalVar.closeSave.password_operate = tbx_password_operate.Text;
            GlobalVar.closeSave.password_super = tbx_password_super.Text;
            GlobalVar.closeSave.password_tech = tbx_password_tech.Text;
            GlobalVar.closeSave.IsSaftGate = ckb_IsSaftGate.Checked;
            SerializeHelper.serializeFile(GlobalVar.closeSavePath, GlobalVar.closeSave, "已保存closeSave配置");
        }
    }
}
